package com.yztc.orangeentertainment.ui.main.articlecommentall;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.litesuits.orm.db.DataBase;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.ArticleCommentAllAdapter;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.bean.articlecommentall.ArticleCommentAllListBean;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/14.
 */
public class ArticleCommentAllActivity extends BaseActivity implements ArticleCommentAllContract.View {

    @InjectView(R.id.ib_articlecommentallback)
    ImageButton ibArticlecommentallback;
    @InjectView(R.id.rv_articlecommentall)
    RecyclerView rvArticlecommentall;
    @InjectView(R.id.srfl_articlecommentall)
    SwipeRefreshLayout srflArticlecommentall;
    private ArticleCommentAllPresenter presenter;
    private int id, page=1;
    private ArticleCommentAllAdapter adapter=new ArticleCommentAllAdapter(this);
    private DataBase dataBase;
    private int lastVisibleItem;
    private LinearLayoutManager linearLayoutManager;
    private List<ArticleCommentAllListBean> listBeen = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.articlecommentall;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.inject(this);
        Intent intent = getIntent();
        //获取传过来的文章id
        id = Integer.parseInt(intent.getStringExtra("id"));
        initData();
        initView();
    }

    /**
     * 通过presenter连接model联网获取数据
     */
    private void initData() {
        presenter = new ArticleCommentAllPresenter(this);

        presenter.getArticleCommentAllFromNet(this, id, page);
    }

    private void initView() {
        linearLayoutManager = new LinearLayoutManager(this);
        //垂直摆放
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //间距
        rvArticlecommentall.addItemDecoration(new DividerItemDecoration(5));
        rvArticlecommentall.setLayoutManager(linearLayoutManager);
        //swipeRefreshLayout下拉刷新
        srflArticlecommentall.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                        adapter.notifyDataSetChanged();
                        srflArticlecommentall.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        //点击按钮返回前关掉当前页面
        ibArticlecommentallback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //判断是否滑到最低端或者是时最后一个 item的下标加1是否等于当前adapter中数据的个数
        rvArticlecommentall.addOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem + 1) ==adapter
                        .getItemCount()){
                    //下一页
                    page++;
                    //显示刷新动画
                    srflArticlecommentall.setRefreshing(true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            srflArticlecommentall.setRefreshing(true);
                            initData();
                            adapter.notifyDataSetChanged();
                            srflArticlecommentall.setRefreshing(false);
                        }
                    } , 2000);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //得到最后一个item的下标
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });
    }
    //获取数据成功
    @Override
    public void onArticleCommentAllSucess(List<ArticleCommentAllListBean> infoBeanList) {
        if(infoBeanList!=null) {
            listBeen.addAll(infoBeanList);
            adapter.setList(listBeen);
            if (page == 1) {
                rvArticlecommentall.setAdapter(adapter);
            } else {
                adapter.notifyDataSetChanged();
            }
        }

    }
    //获取数据失败
    @Override
    public void onArticleCommentAllFaild(String msg) {
        Toast.makeText(ArticleCommentAllActivity.this,msg,Toast.LENGTH_LONG).show();
    }
}
